// pages/my/address/address.js
var app = getApp();
var dealtype;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    dealtype = '';
    if (options.type) {
      dealtype = options.type;
    }
    console.log(dealtype);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadAddresslist();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addNewAddress: function () {
    wx.navigateTo({
      url: '../addaddr/addaddr?type=add',
    });
  },
  editAddress: function (e) {
    var address = e.currentTarget.dataset.address;
    var address_str = encodeURIComponent(JSON.stringify(address));
    wx.navigateTo({
      url: '../addaddr/addaddr?type=edit&address=' + address_str,
    });
  },
  addressDeal: function (e) {
    var address = e.currentTarget.dataset.address;
    if (dealtype == 'select') {
      app.globalData.selectAddress.status = 1;
      app.globalData.selectAddress.value = address;
      wx.navigateBack({});
    }
    else {
      var address_str = encodeURIComponent(JSON.stringify(address));
      wx.navigateTo({
        url: '../addaddr/addaddr?type=edit&address=' + address_str,
      });
    }
  },
  changeDefault: function (e) {
    console.log('change default');
    var addressid = e.currentTarget.dataset.addressid;
    var url = '/yuanmai_we/usercenter/address/default';
    var params = {
      ID: addressid,
      __zaofans: true
    };
    app.loadDataFromServer(url,this.changeDefaultCallBack,params,'post',true);
  },
  changeDefaultCallBack:function(res){
    this.loadAddresslist();
    app.getAddress();//---更新缓存地址----
  },
  loadAddresslist: function () {
    app.appShowLoading(30000);
    var url = '/yuanmai_we/usercenter/addresses';
    app.loadDataFromServer(url, this.loadAddresslistCallBack, {}, 'get', true);
  },
  loadAddresslistCallBack: function (res) {
    wx.hideToast();
    this.setData({
      addresslist: res
    });
  }
})